package com.puyuba.puyuba.utils.custommessage;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.widget.TextView;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hyphenate.chat.EMCustomMessageBody;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.puyuba.puyuba.R;
import com.puyuba.puyuba.SkinApplication;
import com.puyuba.puyuba.network.Constants;
import com.pyb.java.commonutil.ToastHelper;
import com.pyb.java.cache.SPHelper;
import com.pyb.java.commonutil.utils.FileUtils;


import java.util.List;
import java.util.Map;

public class ShowPhoneTxtNew extends EaseChatRow {
    private TextView tvChatcontent;
    private TextView tvPhone;
    private TextView tvCall;
    private String phone;

    public ShowPhoneTxtNew(Context context, boolean isSender) {
        super(context, isSender);
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(isSender ? R.layout.ease_show_phone_sent : R.layout.ease_show_phone_received, this);
    }

    @Override
    protected void onFindViewById() {
        tvChatcontent = (TextView) findViewById(R.id.tv_chatcontent);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvCall = (TextView) findViewById(R.id.tv_call);

        tvCall.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (FileUtils.isHasSimCard(SkinApplication.getInstance())) {
                    if (!(context.checkSelfPermission(Manifest.permission.MANAGE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            //拨打老师电话
                            XXPermissions.with(context)
                                    // 申请单个打电话权限
                                    .permission(Permission.CALL_PHONE)
                                    .request(new OnPermissionCallback() {

                                        @Override
                                        public void onGranted(List<String> permissions, boolean all) {
                                            if (all) {
                                                //toast("获取录音和日历权限成功");
                                                callPhone(phone);
                                            } else {
                                                //"获取部分权限成功，但部分权限未正常授予");
                                            }
                                        }

                                        @Override
                                        public void onDenied(List<String> permissions, boolean never) {
                                            if (never) {
                                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                                XXPermissions.startPermissionActivity(SkinApplication.getInstance(), permissions);
                                            } else {
                                                //toast("获取录音和日历权限失败");
                                            }
                                        }
                                    });
                        }
                    } else {
                        callPhone(phone);
                    }
                } else {
                    ToastHelper.show("当前设备未插SIM卡~");
                }
            }
        });
    }

    @Override
    protected void onSetUpView() {
        EMCustomMessageBody txtBody = (EMCustomMessageBody) message.getBody();
        Map<String,String> params = txtBody.getParams();
//        String name = params.get(Constants.USER_NAME);
        phone = params.get(Constants.PHONE);
        tvChatcontent.setText(SPHelper.getString("conversationId","") +"的手机号：");
        tvPhone.setText(phone);
    }

    /**
     * 拨打电话
     */
    public void callPhone(String phone) {
        if (phone == null) {
            ToastHelper.show("请重新获取数据");
            return;
        }

        Intent Intent = new Intent(android.content.Intent.ACTION_DIAL,
                Uri.parse("tel:" + phone));//跳转到拨号界面，同时传递电话号码
        context.startActivity(Intent);
    }




}
